package cn.ac.cintcm.prespring.cluster.name;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.solr.core.Config;

import cn.ac.cintcm.prespring.cluster.base.Similarity;

public abstract class NameSimFj {

	protected String fjUrl1 = Config.getInstanceDir()+ "data/originalData/PRES_FJ_NAME.txt";
	protected String outputText = "";

	public NameSimFj(float simRate) {
		Similarity.simRate = simRate;
	}

	public void setFjUrl1(String fjUrl1) {
		this.fjUrl1 = fjUrl1;
	}

	public void setOutputText(String outputText) {
		this.outputText = outputText;
	}

	public boolean isSim(String a, String b) {
		return false;
	}

	/**
	 * not used in name similarity based on frequency and dictionary;
	 * used in name similarity based on LCS and ED
	 * @return similar name, but not the same name FangJi
	 */
	public Map<String, List<String>> calculate(Map<String, String> map)
			throws Exception {
		if (map == null || map.size() == 0) {
			return null;
		}
		Map<String, List<String>> returnMap = new HashMap<String, List<String>>();
		Set<String> keys = map.keySet();
		Iterator<String> iter = keys.iterator();
		int count = 0;
		while (iter.hasNext()) {
			String key = iter.next();
			if ((++count) % 100 == 0) {
				System.out.println("now map size is " + map.size()
						+ " fangji name is " + key);
			}
			String value = map.get(key);
			while (iter.hasNext()) {				
				String k = iter.next().toString();
				String v = map.get(k);
				boolean isSim = isSim(key, k);
				if (isSim) {
					String[] values=value.split(",");
					String[] vs=v.split(",");
					for(int i=0;i<values.length;i++){
						String id=values[i];
						for(int j=0;j<vs.length;j++){
							String simId=vs[j];
							if (returnMap.get(id) == null) {
								List<String> result = new ArrayList<String>();
								result.add(simId);
								returnMap.put(id, result);
							} else {
								returnMap.get(id).add(simId);
							}
						}						
					}
					
					for(int i=0;i<vs.length;i++){
						String id=vs[i];
						for(int j=0;j<values.length;j++){
							String simId=values[j];
							if (returnMap.get(id) == null) {
								List<String> result = new ArrayList<String>();
								result.add(simId);
								returnMap.put(id, result);
							} else {
								returnMap.get(id).add(simId);
							}
						}						
					}
				}
			}
			map.remove(key);
			iter = map.keySet().iterator();
		}
		map.clear();
		map = null;

		return returnMap;
	}

	/**
	 * not used in name similarity based on frequency and dictionary;
	 * used in name similarity based on LCS and ED
	 * @return map(FjName,fangjiIds)
	 */
	public Map<String, String> readFromText() throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				new FileInputStream(fjUrl1), "UTF8"));
		String line = "";
		Map<String, String> map = new HashMap<String, String>();
		while ((line = reader.readLine()) != null) {
			String[] s = line.split(":");
			if (s.length == 2) {
				String fjName= s[0];
				String fjIds = s[1];
				map.put(fjName,fjIds);
				System.out.println(fjName+" ----- "+fjIds);
			}
		}	
		reader.close();
		return map;
	}

	public void writeSimToText(Map<String, List<String>> map) throws Exception {
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(outputText, false), "UTF8"));
		StringBuilder sb = new StringBuilder();
		Set<String> keys = map.keySet();
		Iterator iter = keys.iterator();
		while (iter.hasNext()) {
			String fjId = iter.next().toString();
			sb.append(fjId + ":");
			List<String> simFjIds = map.get(fjId);
			Iterator iter2 = simFjIds.iterator();
			while (iter2.hasNext()) {
				String simFjId = iter2.next().toString();
				sb.append(simFjId + ",");
			}
			if (sb.length() > 0) {
				sb.deleteCharAt(sb.length() - 1);
			}
			//System.out.println(sb.toString());
			writer.append(sb.toString());
			writer.write("\r\n");
			sb.setLength(0);
		}
		writer.close();
		map.clear();
		map = null;
	}
}
